<!-- src/views/Register.vue -->
<template>
    <StartLayout>
        <h2 class="title">用户注册</h2>
        <p class="subtitle">让配送更简单，让生意更轻松</p>
        <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" class="login-form">
            <el-form-item prop="email">
                <el-input v-model="registerForm.email" placeholder="邮箱" size="large" clearable class="custom-input">
                    <template #prefix>
                        <i class="el-icon-user" />
                    </template>
                </el-input>
            </el-form-item>

            <el-form-item prop="code">
                <div class="sms-code-wrapper">
                    <el-input v-model="registerForm.validCode" placeholder="邮箱验证码" size="large"
                        class="sms-code-input custom-input">
                        <template #prefix>
                            <i class="el-icon-message" />
                        </template>
                    </el-input>
                    <el-button :disabled="smsCodeDisabled" :loading="smsCodeLoading" class="sms-code-button"
                        @click="sendCode">
                        {{ smsCodeText }}
                    </el-button>
                </div>
            </el-form-item>

            <el-form-item prop="password">
                <el-input v-model="registerForm.password" type="password" placeholder="密码" size="large" show-password
                    class="custom-input">
                    <template #prefix>
                        <i class="el-icon-lock" />
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <Button :loading="loading" @click="handleRegister">注册</Button>
            </el-form-item>
        </el-form>
        <div class="login-footer">
            <span>已有有账号？</span>
            <el-link type="primary" :underline="false" @click="gotoLogin">去登录</el-link>
        </div>
    </StartLayout>
</template>

<script setup lang="ts">
import { reactive, ref, computed } from 'vue'
import StartLayout from '@/components/layout/MainLayout.vue'
import { ElMessage } from 'element-plus'
import '@/styles/pages/auth/login.scss'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
import { getEmailCode } from '@/services/api/common'
import { register } from '@/services/api/merchant'

const router = useRouter()

const registerFormRef = ref<FormInstance>()
const loading = ref(false)

// 验证码相关
// 验证码
const smsCodeDisabled = ref(false)
const smsCodeLoading = ref(false)
const smsCodeCountdown = ref(0)

// 表单数据
const registerForm = reactive({
    email: '',
    validCode: '',
    password: ''
})

// 表单验证规则
const registerRules = reactive<FormRules>({
    email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ],
    code: [
        { required: true, message: '请输入邮箱验证码', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
    ]
})

// 监听路由变化
//   watch(route, (newRoute) => {
//     redirect.value = (newRoute.query.redirect as string) ?? null
//   }, { immediate: true })

const smsCodeText = computed(() => {
    if (smsCodeLoading.value) return '发送中...'
    if (smsCodeCountdown.value > 0) return `${smsCodeCountdown.value}秒后重发`
    return '获取验证码'
})

// 发送验证码
const sendCode = async () => {
    if (!registerForm.email) {
        ElMessage.error('请输入邮箱地址')
        return
    }

    // 验证邮箱格式
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    if (!emailRegex.test(registerForm.email)) {
        ElMessage.error('请输入正确的邮箱地址')
        return
    }
    smsCodeLoading.value = true

    // 模拟发送短信验证码
    setTimeout(() => {
        smsCodeLoading.value = false
        smsCodeDisabled.value = true
        smsCodeCountdown.value = 60

        getEmailCode(registerForm.email)
        // 倒计时
        const timer = setInterval(() => {
            smsCodeCountdown.value--
            if (smsCodeCountdown.value <= 0) {
                clearInterval(timer)
                smsCodeDisabled.value = false
            }
        }, 1000)

        ElMessage.success('验证码已发送，请注意查收')
    }, 1000)
}

// 处理注册
const handleRegister = async () => {
    if (!registerFormRef.value) return

    // await registerFormRef.value.validate(async (valid) => {
    //   if (valid) {
    loading.value = true

    const res = await register(registerForm)
    if (res.code !== 200) {
        ElMessage.error(res.msg)
        return
    }
    // 模拟注册成功
    ElMessage.success('注册成功')
    loading.value = false
    gotoLogin()
}

const gotoLogin = () => {
    router.push({ path: '/login' })
}
</script>

<style lang="scss" scoped>
.login-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(120deg, #fff9e6 0%, #ffeaa7 100%);
    position: relative;
    overflow: hidden;

    &::before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 300px;
        background: linear-gradient(135deg, #ff9a00 0%, #ff6b00 100%);
        border-radius: 0 0 50% 50% / 0 0 100% 100%;
        transform: scaleX(1.5);
        z-index: 0;
    }

    .login-card {
        width: 100%;
        max-width: 420px;
        padding: 50px 40px 30px;
        background: #fff;
        border-radius: 20px;
        box-shadow: 0 15px 35px rgba(255, 154, 0, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
        position: relative;
        z-index: 1;
        margin-bottom: 30px;

        .login-header {
            text-align: center;
            margin-bottom: 30px;

            .logo-container {
                display: flex;
                justify-content: center;
                margin-bottom: 20px;

                .logo-image {
                    height: 80px;
                    width: 80px;
                }
            }

            .title {
                color: #333;
                font-size: 26px;
                font-weight: 600;
                margin: 0 0 8px;
                letter-spacing: 1px;
            }

            .subtitle {
                color: #888;
                font-size: 14px;
                margin: 0;
            }
        }

        .login-tabs {
            display: flex;
            background: #f5f7fa;
            border-radius: 12px;
            padding: 4px;
            margin-bottom: 30px;

            .tab-item {
                flex: 1;
                text-align: center;
                padding: 10px 0;
                border-radius: 8px;
                cursor: pointer;
                font-size: 14px;
                color: #666;
                transition: all 0.3s;

                &.active {
                    background: linear-gradient(120deg, #ff9a00, #ff6b00);
                    color: white;
                    font-weight: 500;
                    box-shadow: 0 2px 8px rgba(255, 154, 0, 0.3);
                }
            }
        }
    }
}
</style>